<template>
  <el-container style="height:100%;">
    <el-main>
      <el-row>
        <el-col :span="4" :offset="3">
          <label>所属类目：{{dirName}}</label>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="18" :offset="3">
          <el-form
            :inline="true"
            label-position="left"
            label-width="100px"
            ref="infoForm"
            :model="info"
            :rules="rules"
          >
            <el-form-item label="名称:" prop="name">
              <el-input placeholder="请输入名称" v-model="info.name"></el-input>
            </el-form-item>
            <el-form-item label="编码:" prop="code">
              <el-input placeholder="请输入编码" v-model="info.code"></el-input>
            </el-form-item>
            <el-form-item label="描述信息:" prop="description">
              <el-input
                placeholder="请输入描述信息"
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4}"
                v-model="info.description"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5" :offset="16">
          <el-button type="primary" size="medium" @click="submitForm">保存</el-button>
          <el-button size="medium" @click="resetForm">重置</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16" :offset="4">
          <el-divider></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="2" :offset="3">
          <el-button size="medium" icon="el-icon-plus" @click="addPart">添加</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="18" :offset="3">
          <el-table :data="parts" stripe style="width: 100%">
            <el-table-column prop="name" label="名称">
              <template slot-scope="scope">
                <el-input v-model="scope.row.name" clearable></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="code" label="编码">
              <template slot-scope="scope">
                <el-input v-model="scope.row.code" clearable></el-input>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="150">
              <template slot-scope="scope">
                <el-button-group>
                  <el-button
                    size="mini"
                    icon="el-icon-circle-check"
                    @click.stop="() => savePart(scope)"
                  ></el-button>
                  <el-button size="mini" icon="el-icon-delete" @click.stop="() => delPart(scope)"></el-button>
                </el-button-group>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>
<script>
export default {
  components: {},
  watch: {},
  mounted() {
    this.loadDict();
    this.loadDictPart();
  },
  computed: {},
  data() {
    return {
      id: this.$route.params.id,
      dirId: this.$route.query.dirId,
      dirName: this.$route.query.dirName,
      info: {},
      parts: [],
      pageSize: 5,
      pageNum: 1,
      total: 0,
      rules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
        ],
        code: [
          { required: true, message: "请输入编码", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" }
        ],
        description: [
          { required: false },
          {
            min: 1,
            max: 200,
            message: "长度在 1 到 200 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    loadDict() {
      if (this.id == 0) {
        return;
      }
      let _this = this;
      this.axios
        .post(this.HOST + "/table/get", {
          id: this.id
        })
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
            return;
          }
          _this.info = res.data.data;
        })
        .catch(function(err) {
          _this.$message.error("操作失败" + err);
        });
    },
    loadDictPart() {
      let _this = this;
      this.axios
        .post(this.HOST + "/part/list", {
          dictId: this.id
        })
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
            return;
          }
          _this.parts = res.data.data;
        })
        .catch(function(err) {
          _this.$message.error("操作失败" + err);
        });
    },
    createDict() {
      let _this = this;
      this.axios
        .post(this.HOST + "/table/create", {
          dirId: this.dirId,
          name: this.info.name,
          code: this.info.code,
          description: this.info.description
        })
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
            return;
          }
          _this.id = res.data.data.id;
          _this.loadDict();
        })
        .catch(function(err) {
          _this.$message.error("操作失败" + err);
        });
    },
    updateDict() {
      let _this = this;
      this.axios
        .post(this.HOST + "/table/update", {
          id: this.info.id,
          name: this.info.name,
          code: this.info.code,
          description: this.info.description
        })
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
            return;
          }
          _this.id = res.data.data.id;
          _this.loadDict();
        })
        .catch(function(err) {
          _this.$message.error("操作失败" + err);
        });
    },
    submitForm() {
      let _this = this;
      this.$refs.infoForm.validate(valid => {
        if (!valid) {
          return false;
        }
        if (_this.id == 0) {
          _this.createDict();
        } else {
          _this.updateDict();
        }
      });
    },
    resetForm() {
      this.$refs.infoForm.resetFields();
    },
    addPart() {
      this.parts.push({});
    },
    savePart(scope) {
      let _this = this;
      let row = scope.row;
      let index = scope.$index;
      if (!row.name) {
        this.$message({
          message: "请输入名称",
          type: "warning"
        });
        return;
      }
      if (!row.code) {
        this.$message({
          message: "请输入编码",
          type: "warning"
        });
        return;
      }
      let path = "/part/";
      let param = {};
      if (row.id == null) {
        path += "create";
        param.name = row.name;
        param.code = row.code;
        param.dictId = this.id;
      } else {
        path += "update";
        param.name = row.name;
        param.code = row.code;
        param.id = row.id;
      }
      this.axios
        .post(this.HOST + path, param)
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
            return;
          }
          _this.parts.splice(index, 1, res.data.data);
        })
        .catch(function(err) {
          _this.$message.error("操作失败" + err);
        });
    },
    delPart(scope) {
      let _this = this;
      let row = scope.row;
      let index = scope.$index;
      if (row.id == null) {
        this.parts.splice(index, 1);
        return;
      }
      this.axios
        .post(this.HOST + "/part/delete", {
          id: row.id
        })
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
            return;
          }
          _this.parts.splice(index, 1);
        })
        .catch(function(err) {
          _this.$message.error("操作失败" + err);
        });
    }
  }
};
</script>
<style scoped>
.el-row {
  margin-bottom: 20px;
}
</style>
